const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabledCaptcha:false,
    captchaText:'获取验证码',
    phoneVal:'',
    captchVal:'',
    userNameVal:'',
    passwordVal:'',
    radio:'',
    capText:'',
    userNameText:'',
    phoneText:'',
    passwordText:'',
    radioText:'',
    passwordShow:false,
    phoneFocusBool:false,
    capTextBool:false,
    userNameBool:false,
    passwordTextBool:false,
    siteLogoUrl:'',
    protocolList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.getProtocol()
      this.siteLogo()
  },

  siteLogo(){
    let _this=this
    wx.request({
      url: app.globalData.baseUrl + 'siteName',
      data: {},
      success(res) {
        if (res.data.code == 1000) {
          _this.setData({
            siteLogoUrl: res.data.data.site_logo_1
          })
        }
      }
    })
  },

  boolFocus(e){
    const {name}=e.currentTarget.dataset
    this.setData({
      [name]:true
    })
  },

  boolBlus(e){
    const {name}=e.currentTarget.dataset
    this.setData({
      [name]:false
    })
  },

  passwordShowCon(){
    this.setData({
      passwordShow:!this.data.passwordShow
    })
  },

  onChange(e){
    this.setData({
      radio: e.detail,
    });
  },

  getProtocol(){
    let _this=this
    wx.request({
      url: app.globalData.baseUrl + 'agreementList',
      success(res) {
        console.log(res)
        if (res.data.code == 1000) {
            _this.setData({
              protocolList:res.data.data
            })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  emptyFunc(){

  },


  // 获取验证码
  captchaBtn:function(){
    if(this.data.disabledCaptcha){
      return false
    }
    var that = this;
    var time = 60;
    var interval = null;
    var { phoneVal } = this.data;
    if (phoneVal == ''){
      this.setData({
        phoneText:'请输入手机号'
      })
      return false
    }
    if (!(/^1[3-9]\d{9}$/.test(phoneVal))) {
      this.setData({
        phoneText:'手机号格式错误'
      })
      return false
    }
    this.setData({
      phoneText:'',
    })
    that.setData({
      captchaText: '重新获取(60s)',
      disabledCaptcha: true
    })
    interval = setInterval(function () {
      time--;
      if (time > 0) {
        that.setData({
          captchaText: '重新获取('+time+'s)'
        })
      } else {
        clearInterval(interval);
        that.setData({
          captchaText: '获取验证码',
          disabledCaptcha: false
        })
      }
    }, 1000);
    wx.request({
      url: app.globalData.baseUrl + 'user/sendCode',
      data: {
        phone: phoneVal,
        type: 1
      },
      success(res) {
        console.log(res)
        if (res.data.code == 1000) {
          wx.showToast({
            title: '发送成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 注册
  registeredBtn:function(){
    let { phoneVal, captchVal, userNameVal, passwordVal,radio ,phoneText,capText,userNameText,passwordText,radioText} = this.data;
    if (phoneVal == '') {
      this.setData({
        phoneText:'请输入手机号'
      })
      return false
    }
    if(phoneText){this.setData({phoneText:''})}
    if (captchVal == '') {
      this.setData({
        capText:'请输入验证码'
      })
      return false
    }
    if(capText){this.setData({capText:''})}
    if (userNameVal == '') {
      this.setData({
        userNameText:'请输入用户名'
      })
      return false
    }
    if(userNameText){this.setData({userNameText:''})}
    if (passwordVal == '') {
      this.setData({
        passwordText:'请输入密码'
      })
      return false
    }
    if(passwordText){this.setData({passwordText:''})}
    if(!radio){
      this.setData({
        radioText:'请先同意用户协议'
      })
      return false
    }
    if(radioText){this.setData({radioText:''})}
    wx.request({
      url: app.globalData.baseUrl + 'user/mpRegister',
      data: {
        username: userNameVal,
        password: passwordVal,
        phone: phoneVal,
        code: captchVal,
        source:2
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.code == 1000) {
          wx.showToast({
            title: '注册成功',
            icon: 'success',
            duration: 1500
          })
          setTimeout(function(){
            wx.navigateTo({
              url: '/userPage/pages/login/login'
            })
          },1500)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
